<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Model;

use Admin\Logic\NestedSets;
use Think\Page;

class RoleModel extends \Think\Model
{

    /**
     * 开启批量验证
     * @var bool
     */
    protected $patchValidate = true;

    /**
     * 批量验证规则
     * 1. 商品名必填
     * 2. 商品分类必填
     * 3. 品牌必填
     * 4. 供货商必填
     * 5. 市场价必填,必须是货币
     * 6. 商城价格必填,必须是货币
     * 7. 库存必填,必须是数字
     * ...
     */
    protected $_validate = [
        ['name', 'require', '权限不能为空'],
    ];

    /**
     * 开启自动完成
     * @var array
     */
    protected $_auto = [

    ];

    /**
     * 获取所有商品的列表和分页信息
     * @param array $cond
     * @return array
     */
    public function getList(array $cond = [])
    {
        $cond = array_merge(['status' => 1], $cond);
        $count = $this->where($cond)->count();
        $page_setting = C('PAGE_SETTING');
        $page = new Page($count, $page_setting['PAGE_SIZE']);
        $page->setConfig('theme', $page_setting['PAGE_THEME']);
        $page_html = $page->show();
        $rows = $this->where($cond)->page(I('get.p', 1), $page_setting['PAGE_SIZE'])->select();

        return compact('rows', 'page_html');
    }

    public function getPermissions()
    {
        $permissions = $this->select();
        $newPers = [];

        foreach ($permissions as $permission) {
            $newPers[] = [
                'id' => $permission['id'],
                'name' => $permission['name'],
                'parent_id' => $permission['parent_id'],
            ];
        }

        return $newPers;
    }

    public function addRole()
    {
        unset($this->data[$this->getPk()]);
        $this->startTrans();
        // 保存基本信息
        $role_id = $this->add();

        if ($role_id === false) {
            $this->rollback();
            return false;
        }
        // 保存权限
        $role_permission_model = M('RolePermission');
        $permission_id = I('post.permission_id');
        $data = [];

        foreach ($permission_id as $id) {
            $data[] = [
                'role_id' => $role_id,
                'permission_id' => $id
            ];
        }

        if ($role_permission_model->addAll($data) === false) {
            $this->rollback();
            return false;
        }

        $this->commit();
        return true;

    }

    public function getRolePermissions($id)
    {
        $row = $this->find($id);
        $permission_ids = M('RolePermission')->where(['role_id' => $id])->getField('permission_id', true);
        $row['permission_ids'] = json_encode($permission_ids);
        //var_dump($row);exit;
        return $row;
    }

    public function saveRole($id)
    {
        // 开启事务
        $this->startTrans();
        // 1.保存基本信息
        if ($this->save() === false) {
            $this->rollback();
            return false;
        }
        // 2.保存关联的权限
        $permission_id = I('post.permission_id');
        $role_permission = M('RolePermission');

        if ($role_permission->where(['role_id' => $id])->delete() === false) {
            $this->rollback();
            return false;
        }

        $data = [];

        foreach ($permission_id as $item) {
            $data[] = [
                'role_id' => $id,
                'permission_id' => $item
            ];
        }

        if ($role_permission->addAll($data) === false) {
            $this->rollback();
            return false;
        }

        $this->commit();
        return true;

    }

    public function deleteRole($id)
    {
        $this->startTrans();

        // 1.删除角色表
        if($this->delete($id)===false){
            $this->rollback();
            return false;
        }

        // 2.删除角色权限表
        if(M('RolePermission')->where(['role_id'=>$id])->delete()===false){
            $this->rollback();
            return false;
        }

        $this->commit();
        return true;
    }
}
